package com.gobang.controller;

import com.gobang.domain.vo.Result;
import com.gobang.mapper.UserMapper;
import com.gobang.util.RedisUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

/**
 * 连接测试控制器
 * 用于测试MySQL、Redis、RabbitMQ连接是否正常
 */
@RestController
@RequestMapping("/api/test")
public class ConnectionTestController {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserMapper userMapper;

    /**
     * 测试所有连接
     */
    @GetMapping("/all")
    public Result<Map<String, Object>> testAll() {
        Map<String, Object> result = new HashMap<>();

        // 测试MySQL连接
        result.put("mysql", testMySQL());

        // 测试Redis连接
        result.put("redis", testRedis());

        // 测试RabbitMQ连接
        result.put("rabbitmq", testRabbitMQ());

        return Result.success(result);
    }

    /**
     * 测试MySQL连接
     */
    @GetMapping("/mysql")
    public Result<Map<String, Object>> testMySQL() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试数据库连接
            Connection connection = dataSource.getConnection();
            result.put("connected", connection != null && !connection.isClosed());
            result.put("database", connection.getCatalog());
            result.put("url", connection.getMetaData().getURL());
            
            // 测试查询用户表
            Long userCount = userMapper.selectCount(null);
            result.put("userCount", userCount);
            
            connection.close();
            result.put("status", "success");
            result.put("message", "MySQL连接成功");
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "MySQL连接失败: " + e.getMessage());
            result.put("connected", false);
        }
        return Result.success(result);
    }

    /**
     * 测试Redis连接
     */
    @GetMapping("/redis")
    public Result<Map<String, Object>> testRedis() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试写入
            String testKey = "gobang:test:key";
            String testValue = "Hello Redis!";
            redisUtil.set(testKey, testValue, 60);
            
            // 测试读取
            String getValue = redisUtil.get(testKey);
            
            result.put("connected", testValue.equals(getValue));
            result.put("testKey", testKey);
            result.put("testValue", testValue);
            result.put("getValue", getValue);
            result.put("status", "success");
            result.put("message", "Redis连接成功");
            
            // 清理测试数据
            redisUtil.delete(testKey);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "Redis连接失败: " + e.getMessage());
            result.put("connected", false);
        }
        return Result.success(result);
    }

    /**
     * 测试RabbitMQ连接
     */
    @GetMapping("/rabbitmq")
    public Result<Map<String, Object>> testRabbitMQ() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试发送消息
            String queueName = "gobang.test.queue";
            String testMessage = "Hello RabbitMQ!";
            rabbitTemplate.convertAndSend(queueName, testMessage);
            
            result.put("connected", true);
            result.put("queueName", queueName);
            result.put("testMessage", testMessage);
            result.put("status", "success");
            result.put("message", "RabbitMQ连接成功，测试消息已发送");
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "RabbitMQ连接失败: " + e.getMessage());
            result.put("connected", false);
        }
        return Result.success(result);
    }
}

